package com.app.view_controller;

import java.util.List;
import java.util.Stack;

import com.app.alarm.R;
import com.app.model.AlarmDataManager;
import com.app.util.ApplicationMediator;
import com.app.util.MessageListenerAdapter;

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.res.Configuration;
import android.graphics.Color;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.Window;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.ViewAnimator;

@SuppressWarnings("unused")
public class ContentActivity extends Activity implements OnGlobalLayoutListener {

	protected static final boolean DEBUG = true;
	protected static final String TAG = "CONTENTACTIVITY";

	public static final String CELL_BOUNDS = "cellbounds";
	public static final String LIST_BOUNDS = "listbounds";
	public static final String EXIT_CALL = "exitcall";
	public static final String SET_CALL = "setcall";
	protected static final String TO_ACTIVITY_ANIMATION_DATA = "animationdata";
	protected static final String TO_DIALOG_ANIMATION_DATA = "todialoganimationdata";
	protected static final String START_DIALOG_CLOSURE = "removelistoverlaycolor";
	protected static final String CELL_CLICK = "cellclick";
	public static final String ALARM_MESSAGE = "allarmmessage";
	public static final String STACK_STATE = "stackstate";
	private static final String DIALOG_VISIBLE = "dialog visible";
	public static final String SET_COLOR_FILTER = "setcolorfilter";
	public static final String LIST_FRAGMENT_TAG = "listtag";
	public static final String ALARM_DIALOG_TAG = "dialogtag";
	public static final String IS_COLORFILTER_VISIBLE = "visible";
	public static final String RECEIVE_TIME_FROM_TIME_PICKER = "rtdtp";
	public static final String TO_DIALOG_ALARM_DATA = "alarmdata";
	protected static final String UPDATE_LIST = "update list";
	public static final int ALARMTONE_PICKER_INTENT_CODE = 666;
	public static final String BACK_BUTTON_PRESSED = "backbuttonpressed";
	public static final String UPDATE_ALARMLIST_AND_DIALOG = "update alarm list and dialog";
	public static final String HIDE_CLICKED_CELL = "hide clicked cell";
	public static final int ANIMATION_DURATION_SHORT = 250;
	public static final int ANIMATION_DURATION_MEDIUM = 500;
	public static final int ANIMATION_DURATION_LONG = 1000;
	public static final String MISCELLANEOUS_SHARED_PREFERENCES = "miscellaneous shared preferences";
	public static final String IS_CONTENTACTIVITY_FOREGROUND = "is content activity foreground";

	private boolean firstStart = true;
	private AlarmSetterDialog mAlarmDialog;
	private ListFragment mListFrag;
	private ApplicationMediator mMediator;
	private Stack<String> mFragmentStack;
	private RelativeLayout mActionBar;
	private boolean onCreateCalled = false;

	@SuppressWarnings("unchecked")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_content);
		if (DEBUG)
			Log.d(TAG, "faccio il oncreate");
		mActionBar = (RelativeLayout) findViewById(R.id.action_bar_l_style);
		mMediator = ApplicationMediator.getInstance();
		mAlarmDialog = new AlarmSetterDialog();
		mFragmentStack = new Stack<String>();
		boolean wasVisible = false;
		if (savedInstanceState != null) {
			List<String> list = (List<String>) savedInstanceState.getSerializable(STACK_STATE);
			mFragmentStack.addAll(list);
			wasVisible = savedInstanceState.getBoolean(DIALOG_VISIBLE);
		}
		if (mFragmentStack.size() > 1) {
			FragmentTransaction ft = getFragmentManager().beginTransaction();
			mAlarmDialog = (AlarmSetterDialog) getFragmentManager().findFragmentByTag(mFragmentStack.peek());
			if (DEBUG)
				Log.d(TAG, "malarmdialog vale   " + mAlarmDialog);
			if (!wasVisible && mAlarmDialog != null)
				ft.remove(mAlarmDialog);
			ft.commit();
		}
		if (mFragmentStack.isEmpty()) {
			FragmentTransaction ft = getFragmentManager().beginTransaction();
			mListFrag = new ListFragment();
			ft.add(R.id.activity_holder, mListFrag, LIST_FRAGMENT_TAG);
			mFragmentStack.add(LIST_FRAGMENT_TAG);
			ft.commit();
			if (DEBUG)
				Log.d(TAG, "mfragment stack vale " + mFragmentStack.size());
		}

		mActionBar.getViewTreeObserver().addOnGlobalLayoutListener(this);
		onCreateCalled = true;

		mMediator.setMessagesListener(this, TO_ACTIVITY_ANIMATION_DATA, new MessageListenerAdapter() {

			@Override
			public void onMessageReceivedWithData(Bundle data) {

				FragmentTransaction ft = getFragmentManager().beginTransaction();
				// if (!mAlarmDialog.isAdded()) {
				mAlarmDialog = new AlarmSetterDialog();
				ft.setCustomAnimations(R.animator.request_fragment_transition_animation, 0);
				mAlarmDialog.setArguments(data);
				ft.add(R.id.activity_holder, mAlarmDialog, ALARM_DIALOG_TAG);
				mFragmentStack.add(ALARM_DIALOG_TAG);
				// }
				/*
				 * else {
				 * mMediator.sendMessageWithAttachedData(TO_DIALOG_ANIMATION_DATA
				 * , data); ft.show(mAlarmDialog); }
				 */
				ft.commit();
			}
		});
		mMediator.setMessagesListener(this, ContentActivity.EXIT_CALL, new MessageListenerAdapter() {
			@Override
			public void onMessageReceived() {
				FragmentTransaction ft = getFragmentManager().beginTransaction();
				if (mAlarmDialog.isVisible())
					ft.remove(mAlarmDialog);
				ft.commit();
			}
		});
	}

	@Override
	protected void onResume() {
		getSharedPreferences(MISCELLANEOUS_SHARED_PREFERENCES, MODE_PRIVATE).edit().putBoolean(IS_CONTENTACTIVITY_FOREGROUND, true).commit();
		super.onResume();
	}

	@Override
	public void onGlobalLayout() {
		if (onCreateCalled) {
			mActionBar.setY(-mActionBar.getHeight());
			mActionBar.animate().translationYBy(mActionBar.getHeight());
		}
		onCreateCalled = false;
	}

	public RelativeLayout getApplicationActionBar() {
		return mActionBar;
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		outState.putBoolean("IStart", firstStart);
		if (mAlarmDialog != null)
			outState.putBoolean(DIALOG_VISIBLE, mAlarmDialog.isVisible());
		outState.putSerializable(STACK_STATE, mFragmentStack);
		super.onSaveInstanceState(outState);
	}

	@Override
	public void onBackPressed() {
		if (!mAlarmDialog.isVisible()) { // TODO sistemare non chiamato dopo
											// aver aperto dialog
			super.onBackPressed();
		} else {
			mMediator.sendMessage(BACK_BUTTON_PRESSED);
		}
	}

	@Override
	protected void onPause() {
		if (DEBUG)
			Log.d(TAG, "CONTENTACTIVITY onPauseCalled ");

		super.onPause();
	}

	@Override
	protected void onStop() {
		if (DEBUG)
			Log.d(TAG, "CONTENTACTIVITY onStopCalled ");
		getSharedPreferences(MISCELLANEOUS_SHARED_PREFERENCES, MODE_PRIVATE).edit().putBoolean(IS_CONTENTACTIVITY_FOREGROUND, false).commit();
		super.onStop();
	}
}
